package com.ble.fragment;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

import com.ble.BleDetailActivity;
import com.ble.R;
import com.ble.base.BaseFragment;
import com.ble.utlis.CmdUtlis;
import com.ble.utlis.TextTools;
import com.ble.utlis.ToastUtil;
import com.ble.weight.MyAlertDialog;

import java.util.HashMap;
import java.util.Map;

public class SettingFragment extends BaseFragment implements View.OnClickListener {
    Spinner sp_00010;
    private EditText et_40001;
    private EditText et_40004;
    private EditText et_40005;
    private EditText et_40007;
    private EditText et_40008;
    private EditText et_40010;
    private EditText et_40011;
    private EditText et_40012;
    private EditText et_40017;
    private EditText et_40023;


    Map<Integer, TextView> map = new HashMap<>();
    private EditText et_40024;
    private EditText et_40018;
    private EditText et_40015;
    private EditText et_40009;
    private EditText et_40006;
    private EditText et_40002;
    private Spinner sp_00011;
    private Spinner sp_00012;
    private Spinner sp_00013;
    private Spinner sp_00014;


    @Override
    protected int getContentResid() {
        return R.layout.fragment_setting;
    }

    @Override
    protected void init(View view) {
        sp_00010 = view.findViewById(R.id.sp_00010);
        sp_00011 = view.findViewById(R.id.sp_00011);
        sp_00012 = view.findViewById(R.id.sp_00012);
        sp_00013 = view.findViewById(R.id.sp_00013);
        sp_00014 = view.findViewById(R.id.sp_00014);

        et_40001 = view.findViewById(R.id.et_40001);
        et_40002 = view.findViewById(R.id.et_40002);
        et_40004 = view.findViewById(R.id.et_40004);
        et_40005 = view.findViewById(R.id.et_40005);
        et_40006 = view.findViewById(R.id.et_40006);
        et_40007 = view.findViewById(R.id.et_40007);
        et_40008 = view.findViewById(R.id.et_40008);
        et_40009 = view.findViewById(R.id.et_40009);
        et_40010 = view.findViewById(R.id.et_40010);
        et_40011 = view.findViewById(R.id.et_40011);
        et_40012 = view.findViewById(R.id.et_40012);
        et_40015 = view.findViewById(R.id.et_40015);
        et_40017 = view.findViewById(R.id.et_40017);
        et_40018 = view.findViewById(R.id.et_40018);
        et_40023 = view.findViewById(R.id.et_40023);
        et_40024 = view.findViewById(R.id.et_40024);


        TextView tv_40001 = view.findViewById(R.id.tv_40001);
        TextView tv_40002 = view.findViewById(R.id.tv_40002);
        TextView tv_40004 = view.findViewById(R.id.tv_40004);
        TextView tv_40005 = view.findViewById(R.id.tv_40005);
        TextView tv_40006 = view.findViewById(R.id.tv_40006);
        TextView tv_40007 = view.findViewById(R.id.tv_40007);
        TextView tv_40008 = view.findViewById(R.id.tv_40008);
        TextView tv_40009 = view.findViewById(R.id.tv_40009);
        TextView tv_40010 = view.findViewById(R.id.tv_40010);
        TextView tv_40011 = view.findViewById(R.id.tv_40011);
        TextView tv_40012 = view.findViewById(R.id.tv_40012);
        TextView tv_40015 = view.findViewById(R.id.tv_40015);
        TextView tv_40017 = view.findViewById(R.id.tv_40017);
        TextView tv_40018 = view.findViewById(R.id.tv_40018);
        TextView tv_40023 = view.findViewById(R.id.tv_40023);
        TextView tv_40024 = view.findViewById(R.id.tv_40024);

        Button btn_00010 = view.findViewById(R.id.btn_00010);
        Button btn_00011 = view.findViewById(R.id.btn_00011);
        Button btn_00012 = view.findViewById(R.id.btn_00012);
        Button btn_00013 = view.findViewById(R.id.btn_00013);
        Button btn_00014 = view.findViewById(R.id.btn_00014);

        Button btn_40001 = view.findViewById(R.id.btn_40001);
        Button btn_40002 = view.findViewById(R.id.btn_40002);
        Button btn_40004 = view.findViewById(R.id.btn_40004);
        Button btn_40005 = view.findViewById(R.id.btn_40005);
        Button btn_40006 = view.findViewById(R.id.btn_40006);
        Button btn_40007 = view.findViewById(R.id.btn_40007);
        Button btn_40008 = view.findViewById(R.id.btn_40008);
        Button btn_40009 = view.findViewById(R.id.btn_40009);
        Button btn_40010 = view.findViewById(R.id.btn_40010);
        Button btn_40011 = view.findViewById(R.id.btn_40011);
        Button btn_40012 = view.findViewById(R.id.btn_40012);
        Button btn_40015 = view.findViewById(R.id.btn_40015);
        Button btn_40017 = view.findViewById(R.id.btn_40017);
        Button btn_40018 = view.findViewById(R.id.btn_40018);
        Button btn_40023 = view.findViewById(R.id.btn_40023);
        Button btn_40024 = view.findViewById(R.id.btn_40024);


        btn_00010.setOnClickListener(this);
        btn_00011.setOnClickListener(this);
        btn_00012.setOnClickListener(this);
        btn_00013.setOnClickListener(this);
        btn_00014.setOnClickListener(this);

        btn_40001.setOnClickListener(this);
        btn_40002.setOnClickListener(this);
        btn_40004.setOnClickListener(this);
        btn_40005.setOnClickListener(this);
        btn_40006.setOnClickListener(this);
        btn_40007.setOnClickListener(this);
        btn_40008.setOnClickListener(this);
        btn_40009.setOnClickListener(this);
        btn_40010.setOnClickListener(this);
        btn_40011.setOnClickListener(this);
        btn_40012.setOnClickListener(this);
        btn_40015.setOnClickListener(this);
        btn_40017.setOnClickListener(this);
        btn_40018.setOnClickListener(this);
        btn_40023.setOnClickListener(this);
        btn_40024.setOnClickListener(this);

        map.put(40001, tv_40001);
        map.put(40002, tv_40002);
        map.put(40004, tv_40004);
        map.put(40005, tv_40005);
        map.put(40006, tv_40006);
        map.put(40007, tv_40007);
        map.put(40008, tv_40008);
        map.put(40009, tv_40009);
        map.put(40010, tv_40010);
        map.put(40011, tv_40011);
        map.put(40012, tv_40012);
        map.put(40015, tv_40015);
        map.put(40017, tv_40017);
        map.put(40018, tv_40018);
        map.put(40023, tv_40023);
        map.put(40024, tv_40024);


    }


    @Override
    public void onResume() {
        super.onResume();
        BleDetailActivity activity = (BleDetailActivity) getActivity();
        Map<Integer, String> ssz = activity.getSsz();
        for (Map.Entry<Integer,String> entry : ssz.entrySet()) {
            System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
            TextView textView = map.get(entry.getKey());
            if(textView!=null) {
                textView.setText(entry.getValue());
            }
        }

    }

    public void setValue(int adress, String value, byte[] b_value, Integer index) {
        TextView textView = map.get(adress);
        if (textView != null) {
            textView.setText(value);
        }
//        if(adress==10){
//            sp_00010.setSelection(index);
//        }
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.btn_00010:
                String s = sp_00010.getSelectedItem().toString();
                String ss = "on".equals(s) ? "0001" : "0000";
                sendcmds("强制有火", (byte) 0x05, 10, 0, 1, ss, s);
                break;
            case R.id.btn_00011:
                String s11 = sp_00011.getSelectedItem().toString();
                String ss11 = "on".equals(s11) ? "0001" : "0000";
                sendcmds("复位蓝牙", (byte) 0x05, 11, 0, 1, ss11, s11);
                break;
            case R.id.btn_00012:
                String s12 = sp_00012.getSelectedItem().toString();
                String ss12 = "on".equals(s12) ? "0001" : "0000";
                sendcmds("数码管禁止显示", (byte) 0x05, 12, 0, 1, ss12, s12);
                break;
            case R.id.btn_00013:
                String s13 = sp_00013.getSelectedItem().toString();
                String ss13 = "on".equals(s13) ? "0001" : "0000";
                sendcmds("强制故障信号", (byte) 0x05, 13, 0, 1, ss13, s13);
                break;
            case R.id.btn_00014:
                String s14 = sp_00014.getSelectedItem().toString();
                String ss14 = "on".equals(s14) ? "0001" : "0000";
                sendcmds("参数复位", (byte) 0x05, 14, 0, 1, ss14, s14);
                break;

            case R.id.btn_40001:
                sendCmd("设备地址", (byte) 0x06, 1, 0, 99, et_40001);
                break;
            case R.id.btn_40002:
                sendCmd(" 位置标签", (byte) 0x06, 2, 0, 1000, et_40002);
                break;
            case R.id.btn_40004:
                sendCmd("灵敏度", (byte) 0x06, 4, 1, 1000, et_40004);
                break;
            case R.id.btn_40005:
                sendCmd("强制拉入值", (byte) 0x06, 5, 1, 99, et_40005);
                break;
            case R.id.btn_40006:
                sendCmd("强制脱开值", (byte) 0x06, 6, 1, 99, et_40006);
                break;
            case R.id.btn_40007:
                sendCmd("判别最高频率", (byte) 0x06, 7, 1, 500, et_40007);
                break;
            case R.id.btn_40008:
                sendCmd("判别最低频率", (byte) 0x06, 8, 1, 500, et_40008);
                break;
            case R.id.btn_40009:
                sendCmd("频率能量拉入值", (byte) 0x06, 9, 1, 500, et_40009);
                break;
            case R.id.btn_40010:
                sendCmd("频率能量脱开值", (byte) 0x06, 10, 1, 500, et_40010);
                break;
            case R.id.btn_40011:
                sendCmd("着火延迟", (byte) 0x06, 11, 1, 100, et_40011);
                break;
            case R.id.btn_40012:
                sendCmd("无火延迟", (byte) 0x06, 12, 1, 250, et_40012);
                break;
            case R.id.btn_40015:
                sendCmd("电流输出模式", (byte) 0x06, 15, 0, 1, et_40015);
                break;
            case R.id.btn_40017:
                sendCmd("采样平均最大值", (byte) 0x06, 17, 1, 99, et_40017);
                break;
            case R.id.btn_40018:
                sendCmd("采样平均最小值", (byte) 0x06, 18, 1, 99, et_40018);
                break;
            case R.id.btn_40023:
                sendCmd("自动增益", (byte) 0x06, 23, 0, 1, et_40023);
                break;
            case R.id.btn_40024:
                sendCmd("有火次数", (byte) 0x06, 24, 1, 99, et_40024);
                break;

//            case R.id.btn_40022:
//              //  sendCmd("系统密码", (byte) 0x06,22, 0, 100, et_40022);
//                String value = et_40022.getText().toString().trim();
//                if(value==null||value.length()!=4){
//                    ToastUtil.showShort(self,"密码不合法，请输入四位密码");
//                    return;
//                }
//                sendcmds("系统密码", (byte) 0x06,22, 0, 999999, value,value);
//                break;
//            case R.id.btn_40011:
//              //  sendCmd("系统密码", (byte) 0x06,22, 0, 100, et_40022);
//                String value1 = et_40011.getText().toString().trim();
//                if(value1==null||value1.length()!=4){
//                    ToastUtil.showShort(self,"密码不合法，请输入四位密码");
//                    return;
//                }
//                sendcmds("用户密码", (byte) 0x06,11, 0, 999999, value1,value1);
//                break;

        }
    }

    private void sendCmd(String title, final byte code, int adress, int min, int max, EditText et) {
         adress = (adress - 1);
        final String trim = et.getText().toString().trim();
        sendcmd(title, code, adress, min, max, trim);
    }

    private void sendcmd(String title, final byte code, final int adress, int min, int max, final String trim) {
        if (trim.isEmpty()) {
            ToastUtil.showShort(self, title + "不能为空");
            return;
        }
        Integer integer = Integer.valueOf(trim);
        if (min > integer || max < integer) {
            ToastUtil.showShort(self, title + "设置范围为" + min + "到" + max);
            return;
        }
        final BleDetailActivity bleDetailActivity = (BleDetailActivity) self;
        MyAlertDialog builder = new MyAlertDialog(self).builder();
        builder.setTitle("设置").setMsg("设置" + title + "为" + trim )
                .setPositiveButton("确定", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        byte[] bytes = CmdUtlis.grtCmdSetting2(code, adress, TextTools.intToByte2(Integer.valueOf(trim)));
                        bleDetailActivity.sendCmd(bytes);
                    }
                }).setNegativeButton("取消", null).show();
    }

    private void sendcmds(String title, final byte code, int adress, int min, int max, final String trim, String alert) {
        adress = (adress - 1);
        final BleDetailActivity bleDetailActivity = (BleDetailActivity) self;
        MyAlertDialog builder = new MyAlertDialog(self).builder();
        int finalAdress = adress;
        builder.setTitle("设置").setMsg("设置" + title + "为" + alert)
                .setPositiveButton("确定", view -> {
                    byte[] bytes = CmdUtlis.grtCmdSetting2(code, finalAdress, TextTools.hexString2Bytes(trim));
                    bleDetailActivity.sendCmd(bytes);
                }).setNegativeButton("取消", null).show();
    }
}
